* ======================================
* --------- Import politicians ---------
* ======================================


* Local Politicians
* =================

	* Local candidate dataset
	use "$dta_raw\dta7\W19_0078_LCD_V7_AVID_ED.dta", clear
	keep if running_loc == 1
	keep if year >= 2003 
	rename w19 idPers
	
	* Correct some mistakes
	replace party_loc = usubinstr(party_loc, "`=uchar(65533)'", "o", .) // "ø"
	replace preadvantage_loc = 0 if year == 2019 & party_loc == "sv" ///
		& knr_loc == 1848 & rank_loc == 13 		// Miscoded
		
	* Drop inconsistent observations
	drop if length(idPers) > 12 				// Unmatched with ssb registry
	bysort idPers year: drop if _N > 1 			// Cands. on multiple lists
	bysort year rank_loc knr_loc partyname_loc: drop if _N > 1 // Same spots

	* Create list rank percentile
	bysort knr_loc party_loc year: egen listsize_loc = max(rank_loc)
	gen rankpct_loc = rank_loc / listsize_loc
	
	keep idPers year running_loc elected_loc preadvantage_loc rank_loc rankpct_loc party_loc

	tempfile candidates_loc
	save `candidates_loc'


* Regional politicians
* ====================

	* Local candidate dataset
	use "$dta_raw\dta7\W19_0078_LCD_V7_AVID_ED.dta", clear
	keep if running_reg == 1
	keep if year >= 2003 
	rename w19 idPers
	
	* Drop inconsistent observations
	drop if length(idPers) > 12 			// Unmatched with ssb registry
	bysort idPers year: drop if _N > 1 		// Cands. on multiple lists
	bysort year rank_reg cnr_reg partyname_reg: drop if _N > 1 // Same spots
	
	keep  idPers year running_reg elected_reg

	tempfile candidates_reg
	save `candidates_reg'
	
	
* Merge local and regional
* ========================
	
	clear
	use `candidates_loc'
	merge 1:1 idPers year using `candidates_reg', nogen
	recode running_* (mis = 0) 		 
	
	save "$dta\ImportPoliticians.dta",replace